package com.hukun.ssm1110.controller;

import com.hukun.ssm1110.entity.Bill;
import com.hukun.ssm1110.entity.User;
import com.hukun.ssm1110.mapper.BillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Date;
import java.util.List;

@Controller
public class BillController {


    @Autowired
    BillMapper billMapper;

//    //GET Mapping 映射  里面的参数 用户访问的名称路径
//    @GetMapping("/billList")
//    //控制页面跳转返回值必须是string 指的是 页面名称
//    public String tobillList(Model model) {
//        //model 把后台数据传到前台页面 这个model数据模型对象始终放在服务器容器中
//        List<Bill> bills = billMapper.queryBiils();
//        model.addAttribute("bills", bills);
//        return "billList"; //去找templates 下面的login.html
//    }
    //综合查询的优化过程  查询全部----》条件查询---》合并方法

    @RequestMapping("/billListPage")
    public String getBillByWhere(Bill bill, Model model) {


        List<Bill> bills = billMapper.queryBiilsByWhere(bill);
        model.addAttribute("bills", bills);
        model.addAttribute("where", bill);
        return "billList"; //回到账单页面
    }

    /**
     * 添加账单逻辑
     *
     * @param bill
     * @return
     */

    @RequestMapping("/addBill")
    public String addBill(Bill bill, Model model) {
        bill.setCreateTime(new Date());

        try {
            int i = billMapper.insertBill(bill);
            if (i > 0) {
                //  添加成功之后进行二次查询
                //redirect:/billList controller 请求内部跳转
                return "redirect:/billListPage";
            } else {
                return "billAdd";
            }
        } catch (Exception e) {
            model.addAttribute("addError", "订单编码重复请查证");
            return "billAdd";
        }


    }

    @RequestMapping("/billViewPage")
    public String billView(String orderCode, Model model) {
        Bill bill = billMapper.getBillByID(orderCode);
        model.addAttribute("billByid", bill);
        return "billView";
    }


    @RequestMapping("/billDeleteById")
    public String billDeleteById(String orderCode) {
        int i = billMapper.DeleteBillByID(orderCode);
//     删除后进行二次查询
        return "redirect:/billListPage";


    }


    /**
     * 去指定的页面
     *
     * @return
     */
    @RequestMapping("/billAddPage")
    public String togoBillAddPage() {
        return "billAdd";
    }


    /**
     * 就是修改前先进行查询操作然后把查询结果返回给修改页面
     *
     * @param orderCode
     * @param model
     * @return
     */
    @RequestMapping("/billUpdateForSelect")
    public String getVBillByCode(String orderCode, Model model) {
        Bill billByID = billMapper.getBillByID(orderCode);
        model.addAttribute("bill", billByID);
        return "billUpdate";
    }

    /**
     * 更新操作  1，update 记录  2，更新成功后的查询列表
     *
     * @param bill
     * @param model
     * @return
     */

    @RequestMapping("/billUpdate")
    public String updateBill(Bill bill, Model model) {
        int i = billMapper.updateBillByID(bill);
        if (i > 0) {
            return "redirect:/billListPage";
        } else {
            model.addAttribute("error", "更新错误。。。");
            return "billUpdate";
        }
    }


}
